package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssignRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.atguigu.vo.system.SysRoleSaveVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags="角色相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll(){
        return Result.ok(sysRoleService.list());
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<SysRole>> page(@PathVariable("page")Integer page,
                              @PathVariable("limit")Integer limit, SysRoleQueryVo vo){
        return Result.ok(sysRoleService.pageQuery(page,limit,vo));
    }

    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result<String> save(@RequestBody SysRoleSaveVo saveVo){
        return sysRoleService.saveRole(saveVo);
    }

    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result<String> update(@RequestBody SysRoleSaveVo saveVo){
        return sysRoleService.updateRole(saveVo);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/remove/{id}")
    public Result<String> deleteById(@PathVariable Long id){
        sysRoleService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result<SysRole> getById(@PathVariable Long id){
        return Result.ok(sysRoleService.getById(id));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result<String> deleteByIds(@RequestBody List<Long> ids){
        sysRoleService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("查询某个用户所对应的所有角色")
    @GetMapping("/toAssign/{userId}")
    public Result<Map<String,Object>> toAssign(@PathVariable Long userId){
        return Result.ok(sysRoleService.getRoleByUserId(userId));
    }

    @ApiOperation("为用户绑定角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleVo vo){
        sysRoleService.doAssign(vo);
        return Result.ok();
    }
}
